#!/bin/bash



echo "请选择"
echo "1. 修改为讯飞服务"
echo "2. 修改为字节服务（火山引擎）"
echo "3. 自定义提示词"
read choice

case $choice in
    1)
        llm_server="xun_fei"
        tts_server="xun_fei"
        iat_server="xun_fei"
        echo "请输入讯飞大模型的 appid："
        read appid
        echo "请输入讯飞大模型的 apiSecret："
        read apiSecret
        echo "请输入讯飞大模型的 apiKey："
        read apiKey
       
        

        # 将讯飞的参数保存到 config.txt 文件中
        echo "appid=$appid" > config.txt
        echo "apiSecret=$apiSecret" >> config.txt
        echo "apiKey=$apiKey" >> config.txt

        cat > index.js <<EOF
const espAi = require("esp-ai"); 
const config = { 
    gen_client_config: (params) => {
        return { 
            iat_server: "$iat_server",
            iat_config: {
                appid: "$appid",
                apiSecret: "$apiSecret",
                apiKey: "$apiKey", 
                vad_eos: 1500,
            },
            llm_server: "$llm_server",
            llm_config: {
                appid: "$appid",
                apiSecret: "$apiSecret",
                apiKey: "$apiKey",
                llm: "v4.0",
            },
            tts_server: "$tts_server",
            tts_config: {
                appid: "$appid",
                apiSecret: "$apiSecret",
                apiKey: "$apiKey",
            },
            llm_init_messages: [
                { role: 'system', content: '你是小明同学，是一个无所不能的智能助手。' },
            ],
            intention: [
                {
                    key: ["帮我开灯", "开灯", "打开灯"],
                    instruct: "device_open_001",
                    message: "开啦！还有什么需要帮助的吗？"
                },
                {
                    key: ["帮我关灯", "关灯", "关闭灯"],
                    instruct: "device_close_001",
                    message: "关啦！还有什么需要帮助的吗？"
                },
                {
                    key: ["退下吧", "退下"],
                    instruct: "__sleep__",
                    message: "我先退下了，有需要再叫我。"
                }, 
                {
                    key: async (text = "", llm_historys) => {
                        const regex = /^(播放音乐)(.*)$/;
                        const match = text.match(regex);
                        if (match) {
                            const songName = match[2];
                            console.log("音乐名称:", songName);
                            return songName;
                        } else {
                            return false;
                        }
                    },
                    instruct: "__play_music__",
                    message: "好的！",
                    music_server: async (name, { user_config }) => { 
                        return {
                            url:"[https](https://xiaomingio.top/music.mp3)",
                            seek: 0,
                            message: message
                        };
                    },
                    on_end: (arg) => {
                        console.log(arg);
                    }
                },
            ],
        }
    }
}
espAi(config);
EOF
        echo -e "\033[34m准备重启服务\033[0m"
        docker restart esp-ai
        echo -e "\033[34m重启服务成功\033[0m"
        ;;
        
    2)
        # 读取 config.txt 文件中的参数并正确赋值
        IFS=$'\n' read -r -d '' appid_line apiSecret_line apiKey_line < config.txt
        appid=$(echo "$appid_line" | cut -d'=' -f2)
        apiSecret=$(echo "$apiSecret_line" | cut -d'=' -f2)
        apiKey=$(echo "$apiKey_line" | cut -d'=' -f2)

        llm_server="volcengine"
        tts_server="volcengine"
        iat_server="xun_fei"
        echo "请输入火山引擎的 appid："
        read appid_volcengine
        echo "请输入火山引擎的 accessToken："
        read accessToken
        echo "请输入火山引擎的 voice_type："
        read voice_type
        echo "请输入豆包大模型的 apiKey："
        read appid_doubao
        echo "请输入豆包大模型的 epId："
        read epId
        
        cat > index.js <<EOF
const espAi = require("esp-ai"); 
const config = { 
    gen_client_config: (params) => {
        return { 
            iat_server: "$iat_server",
            iat_config: {
                appid: "$appid",
                apiSecret: "$apiSecret",
                apiKey: "$apiKey", 
                vad_eos: 1500,
            },
            llm_server: "$llm_server",
            llm_config: {
                apiKey: "$appid_doubao",
                epId: "$epId", 
            },
            tts_server: "$tts_server",
            tts_config: {
                appid: "$appid_volcengine",
                accessToken: "$accessToken",
                voice_type: "$voice_type",
            },
            llm_init_messages: [
                { role: 'system', content: '你是小明同学，是一个无所不能的智能助手。' },
            ],
            intention: [
                {
                    key: ["帮我开灯", "开灯", "打开灯"],
                    instruct: "device_open_001",
                    message: "开啦！还有什么需要帮助的吗？"
                },
                {
                    key: ["帮我关灯", "关灯", "关闭灯"],
                    instruct: "device_close_001",
                    message: "关啦！还有什么需要帮助的吗？"
                },
                {
                    key: ["退下吧", "退下"],
                    instruct: "__sleep__",
                    message: "我先退下了，有需要再叫我。"
                }, 
                {
                    key: async (text = "", llm_historys) => {
                        const regex = /^(播放音乐)(.*)$/;
                        const match = text.match(regex);
                        if (match) {
                            const songName = match[2];
                            console.log("音乐名称:", songName);
                            return songName;
                        } else {
                            return false;
                        }
                    },
                    instruct: "__play_music__",
                    message: "好的！",
                    music_server: async (name, { user_config }) => { 
                        return {
                            url:"[https](https://xiaomingio.top/music.mp3)",
                            seek: 0,
                            message: message
                        };
                    },
                    on_end: (arg) => {
                        console.log(arg);
                    }
                },
            ],
        }
    }
}
espAi(config);
EOF
        echo -e "\033[34m准备重启服务\033[0m"
        docker restart esp-ai
        echo -e "\033[34m重启服务成功\033[0m"
        ;;

    3)
        echo "自定义提示词"
        # 获取用户输入的新内容
        read -p "请输入新的提示词：" new_content
        # 使用 sed 命令进行替换
        IFS=$'\n' read -r -d '' new_content < config.txt
        content=$(echo "$content" | cut -d'=' -f2)
        if [ -z "$content" ]; then
            content="你是小明同学，是一个无所不能的智能助手。"
        fi
        sed -i "s/{ role: 'system', content: '$content' }/{ role: 'system', content: '$new_content' }/" index.js
        echo "content=$content" > config.txt
        
        echo -e "\033[34m准备重启服务\033[0m"
        docker restart esp-ai
        echo -e "\033[34m重启服务成功\033[0m"
        ;;
    *)
        echo "无效选择"
        ;;
esac